<?php
/**
 * 发送短信
 * @Author: lf
 * @Date:   2018-07-03T17:11:00+0800
 */
namespace app\admin\controller;

use app\admin\model\mainOperator;
use app\admin\model\NoteM;
use app\admin\model\Channelkey;

class SendNote extends Father
{
	public function index()
	{
		$notem = new NoteM;
		$operators = mainOperator::field('code,desc')->select();
		//是否绑定渠道
		if( empty(session('relate_id')) )
		{
			$chasmsids = [];
		}else{
			$chasmsids = NoteM::field('id,notes')->where('status','1')->where('channel_id',session('relate_id'))->select();
			//处理平台模板id选择
			foreach ($chasmsids as $key => $value) {
				$chasmsids[$key]['notes'] = substr($value['notes'], 0, 30).chr(0).'...';
			}
			
		}

		$this->assign('chasmsids',$chasmsids);
		$this->assign('operators',$operators);
		return $this->fetch();
	}

	public function send_n()
	{
    	//是否渠道账号
    	if(session('user_type') != 'channel')
    	{
    		echo "该账户不是渠道账号";return;
    	}
		//是否绑定渠道
		if( empty(session('relate_id')) )
		{
			echo "该账户没有绑定渠道";return;
		}
		$ope    = trim(input('post.ope'));
		$notes  = trim(input('post.notes'));
		$phones = trim(input('post.phones'));
		$cid    = session('relate_id');

		if(empty($ope) || empty($ope) || empty($ope))
		{
			echo "*号为必填值";return;
		}
		//号码转为数组
		$phonesarr = explode(',', $phones);
		if(count($phonesarr) > 100)
		{
			echo "号码不能超过100个";return;
		}
		foreach ($phonesarr as $key => $value) {
			if(empty($value))
			{
				echo "不能有多余的逗号";return;
			}
			if(!is_numeric($value))
			{
				echo "号码必须为数字";return;
			}
			if( strlen($value) != 11)
			{
				echo "号码必须为11位";return;
			}
		}

		//获取cid

		//设置post数据
		$post_data = array(
		'channelOrderId'     => date('YmdHis').uniqid().rand(10,99),
		'channelCallbackUrl' => "http://123",
		'number'             => $phonesarr,
		'channelSMSId'       => $notes,
		'cid'                => $cid,
		'operator'           => $ope
		);


		//获取key
		$keym = Channelkey::get(['cid'=>$cid]);
		$key  = $keym['cid_key'];

		$tmp  = "channelCallbackUrl={$post_data['channelCallbackUrl']}";
		$tmp  .= "&channelOrderId={$post_data['channelOrderId']}";
		$tmp  .= "&cid={$post_data['cid']}";
		$tmp  .= "&number=[{$phones}]";
		$tmp  .= "&channelSMSId={$post_data['channelSMSId']}";
		$tmp  .= "&operator={$post_data['operator']}";
		$tmp  .= "&key={$key}";
		// return;
		
		$header = array();
		$header[] = 'Authorization:'.md5($tmp);
		$header[] = 'Accept:application/json';
		$header[] = 'Content-Type:application/json;charset=utf-8';
		//初始化
		$curl = curl_init();
		//设置抓取的url
		curl_setopt($curl, CURLOPT_URL, 'http://1.14.234.197:9092/SMS/SMS');
		//设置头文件的信息作为数据流输出
		curl_setopt($curl, CURLOPT_HEADER, 0);
		//设置头文件
		curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
		//设置获取的信息以文件流的形式返回，而不是直接输出。
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);
		//设置post方式提交
		curl_setopt($curl, CURLOPT_POST, 1);
		//设置post数据
		curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($post_data,JSON_UNESCAPED_SLASHES));
		//执行命令
		$data = curl_exec($curl);
		//关闭URL请求
		curl_close($curl);
		return;

	}

	//回调
	public function send_callback()
	{
		$uid     = trim(input('post.uid'));
		$orderId = trim(input('post.orderId'));
		$status  = trim(input('post.status'));		
		$msg     = trim(input('post.msg'));		
		$sign    = trim(input('post.sign'));		
		$phone   = trim(input('post.phone'));	


	}
}
